package edu.gyu.myshop.controller;


import edu.gyu.myshop.model.Goodstype;
import edu.gyu.myshop.service.GoodstypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2024-06-18
 */
@Controller
public class GoodstypeController {
    @Autowired
    GoodstypeService goodstypeService;

    @RequestMapping("/admin/category_management")
    public String categoryManagement(Model model) {
        List<Goodstype> category = goodstypeService.list();
        model.addAttribute("categorys", category);
        return "pages/category_management";
    }

    @RequestMapping("/admin/addCategory")
    public String addCategory(Goodstype goodstype, RedirectAttributes redirectAttributes) {
        goodstypeService.save(goodstype);
        redirectAttributes.addFlashAttribute("msg3", "添加成功！");
        return "redirect:/admin/category_management";
    }

    @RequestMapping("/admin/deleteCategory/{id}")
    public String deleteCategory(@PathVariable("id") int id, RedirectAttributes redirectAttributes) {
        goodstypeService.removeById(id);
        redirectAttributes.addFlashAttribute ("msg3", "删除成功！");
        return "redirect:/admin/category_management";
    }
}
